import time
from django.shortcuts import render, redirect

# Create your views here.
from golf import models
from golf.form import MemberModelForm, YardModelForm


def yardList(req):
    list = models.yardinfo.objects.all()
    return render( req, "golf_yardinfo.html",{"list":list})


def yardAdd(req):
    if req.method == "POST":
        form = YardModelForm( data=req.POST )
        if form.is_valid():
            form.save()
            return redirect( "/golf/yardList/" )
    form = YardModelForm()

    return render( req, "golf_yard_add.html", {"yardInfo": form})


def yardEdit(req,nid):
    if req.method == "POST":
        info = models.yardinfo.objects.filter(id=nid).first()
        form = YardModelForm( data=req.POST, instance=info )
        if form.is_valid():
            form.save()
            return redirect( "/golf/yardList/" )
    info = models.yardinfo.objects.filter(id=nid).first()
    form = YardModelForm( instance=info )
    return render( req, "golf_yard_edit.html",{"form":form})

def yardsales(req):
    sales = models.saleinfo.objects.all()
    return render(req,"golf_yard_saleinfo.html",{"list":sales})